package commons.utils.facades.internal;

import commons.utils.facades.SWTFacade;

public class FacadeFactory {

    public static <T> T createFacade(Class<T> type) {
        final String suffix = SWTFacade.isRAP() ? "RAP" : "RCP";
        final String name = type.getName() + suffix;
        try {
            Class< ? > clazz = type.getClassLoader().loadClass(name);
            return type.cast(clazz.newInstance());
        } catch (ClassCastException cce) {
            String msg = String.format("Class %s is not an instance of %s", name, type.getName());
            throw new RuntimeException(msg);
        } catch (ClassNotFoundException cnfe) {
            String msg = String.format("Class %s was not found", name);
            throw new RuntimeException(msg);
        } catch (IllegalAccessException iae) {
            throw new RuntimeException(iae);
        } catch (InstantiationException ie) {
            throw new RuntimeException(ie);
        }
    }
}
